/*
 * FileName：SysUserCtrl.java 
 * <p>
 * Copyright (c) 2017-2020, <a href="http://www.webcsn.com">hermit (1154808491@qq.com)</a>.
 * <p>
 * Licensed under the GNU General Public License, Version 3 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/gpl-3.0.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package com.smart.primary.sys.ctrl;

import java.util.List;

import com.smart.primary.common.common.BaseCtrl;
import com.smart.primary.common.common.Constants;
import com.smart.primary.common.util.AjaxResult;
import com.smart.primary.sys.service.UserService;
import com.smart.primary.sys.vo.Org;
import com.smart.primary.sys.vo.Role;
import com.smart.primary.sys.vo.User;
import com.smart.primary.sys.vo.UserRole;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.WebUtils;

/**
 * 用户信息表 Controller层
 *
 * @author hermit
 * @date 2018 -02-23 11:02:18
 */
@Controller
@RequestMapping("/sys/user")
public class UserCtrl extends BaseCtrl {
    
    /**
     * ss * The Sys user service.
     */
    @Autowired
    private UserService sysUserService;
    
    /**
     * 获取页面分页数据集合
     *
     * @param sysUser
     * @return
     * @throws Exception
     * @author hermit
     * @date 2018 -02-23 11:02:18
     */
    @RequestMapping("/list")
    @ResponseBody
    public AjaxResult list(User sysUser)
        throws Exception {
        List<User> list = sysUserService.queryListByPage(sysUser);
        return getResult(sysUser, list);
    }
    
    /**
     * 数据保存(新增或修改)
     *
     * @param sysUser
     * @return
     * @throws Exception
     * @author hermit
     * @date 2018 -02-23 11:02:18
     */
    @RequestMapping("/save")
    @ResponseBody
    public AjaxResult save(User sysUser)
        throws Exception {
        System.err.println(sysUser.getOrgNo());
        String msg = sysUserService.formVerify(sysUser, "add");
        if (msg == null) {
            if(StringUtils.isBlank(sysUser.getOrgNo())){
                User uuu = (User) WebUtils.getSessionAttribute(request, Constants.SESSION_USER);
               if(uuu !=null){
                   sysUser.setOrgNo(uuu.getOrgNo());
               }
            }
            sysUserService.saveOrUpdate(sysUser);
        } else {
            return AjaxResult.failure(msg);
        }
        return AjaxResult.success();
    }

    @RequestMapping("/get")
    @ResponseBody
    public AjaxResult get() {
        User sysUser = (User) WebUtils.getSessionAttribute(request, Constants.SESSION_USER);
        String userId = "0";
        if(sysUser != null) {
            userId = sysUser.getIsSystem();
        }
        return AjaxResult.success(userId);
    }
    
    /**
     * 数据删除
     *
     * @param sysUser
     * @return
     * @throws Exception
     * @author hermit
     * @date 2018 -02-23 11:02:18
     */
    @RequestMapping("/delete")
    @ResponseBody
    public AjaxResult delete(User sysUser)
        throws Exception {
        
        sysUserService.delete(sysUser.getUserId());
        
        return AjaxResult.deleteSuccess();
    }
    
    /**
     * @param userId the user id
     * @param sysRole the sys role
     * @return role list by user id
     * @throws Exception the exception
     * @author hermit
     * @date 2018 -02-27 10:04:01
     */
    @RequestMapping("/getrolelist")
    @ResponseBody
    public AjaxResult getRoleListByUserId(String userId, Role sysRole)
        throws Exception {
        List<Role> list = sysUserService.getRoleListByUserId(sysRole, userId);
        return getResult(sysRole, list);
    }
    
    /**
     * 设置用户角色
     *
     * @param sysUserRole
     * @return
     * @throws Exception the exception
     * @author hermit
     * @date 2018 -02-27 10:04:01
     */
    @RequestMapping("/setuserrole")
    @ResponseBody
    public AjaxResult setUserRole(UserRole sysUserRole)
        throws Exception {
        sysUserService.setUserRole(sysUserRole);
        return AjaxResult.success();
    }
    
    /**
     * 删除用户角色
     *
     * @param sysUserRole
     * @return
     * @throws Exception
     * @author hermit
     * @date 2018 -02-27 10:03:51
     */
    @RequestMapping("/deluserrole")
    @ResponseBody
    public AjaxResult delUserRole(UserRole sysUserRole)
        throws Exception {
        sysUserService.delUserRole(sysUserRole);
        return AjaxResult.success();
    }
    
    /**
     * 重置密码
     * 
     * @param sysUser
     * @return
     * @throws Exception
     * @author hermit
     * @date 2018 -02-27 15:04:24
     */
    @RequestMapping("/resetpw")
    @ResponseBody
    public AjaxResult resetPassWord(User sysUser)
        throws Exception {
        sysUserService.resetPassWord(sysUser);
        return AjaxResult.success();
    }
    
    /**
     * 获取机构下拉数据s
     * 
     * @return org select data
     * @author hermit
     * @date 2018 -03-02 09:37:22
     */
    @RequestMapping("/getorgselectdata")
    @ResponseBody
    public AjaxResult getOrgSelectData()
        throws Exception {
        List<Org> list = sysUserService.getOrgSelectData();
        return AjaxResult.success(list);
    }
    
}
